package example.hellosecurity.filter;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Slf4j
@Component
/**
 * springboot filter 最后交给DelegatingFilterProxy 管理
 */
public class AuthFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        log.info("自定义servlet过滤器开始运作");
        HttpServletRequest req = (HttpServletRequest) request;
        String token = req.getHeader("token");
        if (token != null) {
            //    :TODO check token
            log.info("auth success");
            chain.doFilter(request, response);
        } else {
            log.error("auth failed");
        }
    }
}
